<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>无标题文档</title>
    <link href="../../../static/css/style.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../../../static/js/jquery.js"></script>
    <style type="text/css">
        #birthday {
            padding-right: 0;
            height: 32px;
            line-height: 32px;
        }
    </style>
</head>
<script>
    //数据回显
    $(function () {
        var uId = JSON.parse(localStorage.getItem("sysuid2"));
        $.ajax({
            type: "post",
            url: "/users/userDetail2",
            data: {uId: uId},
            dataType: "JSON",
            success: function (rm) {
                $("#uId").val(rm.data.uid)
                $("#uname").val(rm.data.uname)
                $("#upassword").val(rm.data.upassword)
                if (rm.data.sex == 1) {
                    $("#sex1").attr("checked", true);
                } else {
                    $("#sex2").attr("checked", true);
                }
                $("#utelephone").val(rm.data.utelephone);
                $("#birthday").val(rm.data.birthday);
                $("#statusId").val(rm.data.statusId);
                var deptId = rm.data.deptId;
                var jobId = rm.data.jobId;
                //部门
                $.ajax({
                    type: "post",
                    url: "/dept/deptList",
                    data: {},
                    dataType: "JSON",
                    success: function (rm) {
                        var deptStr = '<option value="">请选择</option>';
                        for (let i = 0; i < rm.data.length; i++) {
                            if(rm.data[i].deptState == 1){
                                if (deptId == rm.data[i].deptId) {
                                    deptStr += '<option selected="selected" value="' + rm.data[i].deptId + '">' + rm.data[i].deptName + '</option>';
                                }else {
                                    deptStr += '<option value="' + rm.data[i].deptId + '">' + rm.data[i].deptName + '</option>';
                                }
                            }
                        }
                        $("#deptId").html(deptStr);
                    },
                    error: function (rm) {
                        alert(rm.msg)
                    }
                });
                //职位
                $.ajax({
                    type: "post",
                    url: "/job/jobList",
                    data: {},
                    dataType: "JSON",
                    success: function (rm) {
                        var jobStr2 = '<option value="">请选择</option>';
                        for (let i = 0; i < rm.data.length; i++)
                            if (rm.data[i].jobState == 1){
                            if(jobId==rm.data[i].jobId){
                                jobStr2 += '<option selected="selected" value="' + rm.data[i].jobId + '">' + rm.data[i].jobName + '</option>';
                            }else {
                                jobStr2 += '<option value="' + rm.data[i].jobId + '">' + rm.data[i].jobName + '</option>';
                            }
                        }
                        $("#jobId").html(jobStr2);
                    },
                    error: function (rm) {
                        alert(rm.msg)
                    }
                });
            }
        });
    });

    //部门选择框改变后
    function userDeptUpdate() {
        var jobDeptId = $("#deptId").val();
        // 职位查询
        $.ajax({
            type: "post",
            url: "/job/jobsList",
            data: {jobDeptId: jobDeptId},
            dataType: "JSON",
            success: function (rm) {
                var list = rm.data;
                var jobStr2 = '<option value="">请选择</option>';
                for (let j = 0; j < rm.data.length; j++) {
                    if (rm.data[j].jobState == 1 && jobDeptId == list[j].jobDeptId) {
                        jobStr2 += '<option value="' + list[j].jobId + '">' + list[j].jobName + '</option>';
                    } else {
                        jobStr2 += '';
                    }
                    $("#jobId").html(jobStr2);
                }
            },
            error: function (rm) {
                alert(rm.msg)
            }
        });
    }

    //检验姓名
    function inputUName() {
        var uname = $("#uname").val();
        $.ajax({
            type: "post",//请求类型
            url: "/users/userDetail",//请求的url
            data: {uname: uname},//请求参数
            dataType: "JSON",//ajax接口（请求url）返回的数据类型
            success: function (rm) {//data：返回数据（json对象）
                if (rm.status === 400) {//账号已存在，错误提示
                    $("#msg2").html(rm.msg);
                } else if (rm.status === 200) {//账号可用，正确提示
                    $("#msg2").html(rm.msg);
                } else if (uname === null || uname === "") {
                    $("#msg2").html("请输入用户姓名");
                }
            }
        });
    }

    //保存
    function updatesave() {
        $.ajax({
            type: "post",
            url: "/users/userUpdate",
            data: $("#Form").serializeArray(),
            traditional: true,
            async: true,
            dataType: "JSON",
            success: function () {
                window.location.href = "findByPage";
            },
            error: function (rm) {
                alert(rm.msg);
            }
        });

    }

</script>
<body>
<div class="place"><span>位置：</span>
    <ul class="placeul">
        <li><a href="findByPage">系统管理</a></li>
        <li><a href="findByPage">用户管理</a></li>
        <li><a href="userUpdate">修改</a></li>
    </ul>
</div>
<form id="Form">
    <div class="formbody">
        <div class="formtitle"><span>员工信息</span></div>
        <ul class="forminfo">
            <li>
                <input name="uId" id="uId" type="hidden" value="9527" class="dfinput"/>
            </li>
            <li>
                <label>员工姓名</label>
                <input name="uname" id="uname" type="text" value="唐寅" class="dfinput" onblur="inputUName()"/>
                <i>必填，不能超过30个字符</i>&nbsp;<font color="red" id="msg2"></font>
            </li>
            <li>
                <label>密码</label>
                <input name="upassword" id="upassword" type="password" value="123456" class="dfinput"/>
                <i>必填，不能超过30个字符</i>
            </li>
            <li>
                <label>性别</label>
                <cite>
                    <input name="sex" id="sex1" type="radio" value="1" checked="checked"/>男
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <input name="sex" id="sex2" type="radio" value="2"/>女
                </cite>
            </li>
            <li>
                <label>联系方式</label>
                <input name="utelephone" id="utelephone" type="text" value="17370899727" class="dfinput"/>
                <i>必填，不能超过30个字符</i>
            </li>
            <li>
                <label>出生年月</label>
                <input name="birthday" type="datetime-local" class="dfinput laydate-icon" id="birthday"
                       value="2014-6-25"/>
            </li>

            <li>
                <label>所属部门</label>
                <select name="deptId" class="dfselect" id="deptId" onchange="userDeptUpdate()">
                    <option value="">请选择部门</option>
                    <option value="">研发</option>
                    <option value="">销售</option>
                    <option value="">财务</option>
                </select>
            </li>

            <li>
                <label>职位</label>
                <select name="jobId" class="dfselect" id="jobId">
                    <option value="">经理</option>
                    <option value="">高级工程师</option>
                    <option value="">中级工程师</option>
                    <option value="">初级工程师</option>
                </select>
            </li>
            <li>
                <label>状态</label>
                <select name="statusId" id="statusId" class="dfselect">
                    <option value="1" selected="selected">在职</option>
                    <option value="2">离职</option>
                </select>
            </li>
            <li>
                <label>&nbsp;</label>
                <input name="" type="button" class="btn" value="确认保存" onclick="updatesave()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input name="" type="button" class="btn" value="返回" onclick="window.history.go(-1);"/>
            </li>
        </ul>
    </div>
</form>
<script type="text/javascript">
    !function () {
        laydate.skin('molv');//切换皮肤，请查看skins下面皮肤库
        laydate({elem: '#birthday'});//绑定元素
    }();
</script>
</body>
